package com.nuanshui.heatedloan.baseservice.service;

import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.res.qiniu.Qiniu;
import com.nuanshui.heatedloan.baseservice.dal.mapper.WebLogInfoMapper;
import com.nuanshui.heatedloan.baseservice.facade.WebLogInfoService;
import com.nuanshui.heatedloan.baseservice.model.UserAgent;
import com.nuanshui.heatedloan.baseservice.model.WebLogInfo;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.enums.FileBelong;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(ServiceConstant.WEBLOGINFOSERVICE)
@Transactional(readOnly=true,  value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
public class WebLogInfoServiceImpl extends CommonServiceImpl implements WebLogInfoService {

	@Autowired
	private Qiniu Qiniu;
	
	@Autowired
	private WebLogInfoMapper webLogInfoMapper;
	
	@Transactional(readOnly=false,  value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	@Override
	public void save(String url) {
		
		if(SysStringUtils.isEmpty(url)) return ;
		
		
		WebLogInfo logInfo = new WebLogInfo();
		
		final String bucketName = Qiniu.genBucketName(FileBelong.PRODUCT);
		UserAgent userAgent =  (UserAgent) getRequest().getAttribute(GlobalParam.USER_AGENT);
		
		logInfo.setUrl(Qiniu.qiniuKeyToLocalKey(url, bucketName));
		logInfo.setUserId(userAgent.getUserId());
		logInfo.setPlatformType(userAgent.getClient());
		logInfo.setPlatformVersion(userAgent.getVersion());
		logInfo.setSafeToken(userAgent.getSafeToken());
		logInfo.setClientIp(getRequest().getRemoteAddr());
		
		webLogInfoMapper.save(logInfo);
	}

	@Override
	public void batchSave(List<String> urlList) {
		final String bucketName = Qiniu.genBucketName(FileBelong.PRODUCT);
		UserAgent userAgent =  (UserAgent) getRequest().getAttribute(GlobalParam.USER_AGENT);
		List<WebLogInfo> infoList = new ArrayList<WebLogInfo>(urlList.size());
		for(String url : urlList){
			WebLogInfo logInfo = new WebLogInfo();
//			logInfo.setId(webLogInfoMapper.nextvalKey());
			logInfo.setUrl(Qiniu.qiniuKeyToLocalKey(url, bucketName));
			logInfo.setUserId(userAgent.getUserId());
			logInfo.setPlatformType(userAgent.getClient());
			logInfo.setPlatformVersion(userAgent.getVersion());
			logInfo.setSafeToken(userAgent.getSafeToken());
			logInfo.setClientIp(getRequest().getRemoteAddr());
			infoList.add(logInfo);
		}
		webLogInfoMapper.batchSave(infoList);
	}

//	@Override
//	public void delete(String url) {
//		webLogInfoMapper.delete(url);
//	}
//
//	@Override
//	public List<WebLogInfoVO> loadCurrentUserWebLog() {
//		UserAgent userAgent =  (UserAgent) getServletRequest().getAttribute(GlobalParam.USER_AGENT);
//		return webLogInfoMapper.loadByUserId(userAgent.getUserId());
//	}

}
//=======
//package com.muying.service.log.impl;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import com.muying.dal.bean.GlobalParam;
//import com.muying.dal.bean.UserAgent;
//import com.muying.dal.mapper.log.WebLogInfoMapper;
//import com.muying.dal.model.WebLogInfo;
//import com.muying.dal.vo.WebLogInfoVO;
//import com.muying.framework.res.qiniu.FileBelong;
//import com.muying.framework.res.qiniu.Qiniu;
//import com.muying.service.common.BaseServiceImpl;
//import com.muying.service.log.WebLogInfoService;
//
//@Service("webLogInfoService")
//public class WebLogInfoServiceImpl extends BaseServiceImpl implements WebLogInfoService{
//
//	@Autowired
//	private Qiniu Qiniu;
//	
//	@Autowired
//	private WebLogInfoMapper webLogInfoMapper;
//	
//	@Transactional
//	@Override
//	public void save(String url) {
//		
//		if(SysStringUtils.isEmpty(url)) return ;
//		
//		WebLogInfo logInfo = new WebLogInfo();
//		
//		final String bucketName = Qiniu.genBucketName(FileBelong.PRODUCT);
//		logInfo.setUrl(Qiniu.qiniuKeyToLocalKey(url, bucketName));
//		
//		UserAgent userAgent =  (UserAgent) getServletRequest().getAttribute(GlobalParam.USER_AGENT);
//		logInfo.setUserId(userAgent.getUserId());
//		logInfo.setPlatformType(userAgent.getClient());
//		logInfo.setPlatformVersion(userAgent.getVersion());
//		logInfo.setSafeToken(userAgent.getSafeToken());
//		logInfo.setClientIp(getServletRequest().getRemoteAddr());
//		
//		webLogInfoMapper.save(logInfo);
//	}
//
//	@Override
//	public void batchSave(List<String> urlList) {
//		final String bucketName = Qiniu.genBucketName(FileBelong.PRODUCT);
//		UserAgent userAgent =  (UserAgent) getServletRequest().getAttribute(GlobalParam.USER_AGENT);
//		List<WebLogInfo> infoList = new ArrayList<WebLogInfo>(urlList.size());
//		for(String url : urlList){
//			WebLogInfo logInfo = new WebLogInfo();
////			logInfo.setId(webLogInfoMapper.nextvalKey());
//			logInfo.setUrl(Qiniu.qiniuKeyToLocalKey(url, bucketName));
//			logInfo.setUserId(userAgent.getUserId());
//			logInfo.setPlatformType(userAgent.getClient());
//			logInfo.setPlatformVersion(userAgent.getVersion());
//			logInfo.setSafeToken(userAgent.getSafeToken());
//			logInfo.setClientIp(getServletRequest().getRemoteAddr());
//			infoList.add(logInfo);
//		}
//		webLogInfoMapper.batchSave(infoList);
//	}
//
////	@Override
////	public void delete(String url) {
////		webLogInfoMapper.delete(url);
////	}
////
////	@Override
////	public List<WebLogInfoVO> loadCurrentUserWebLog() {
////		UserAgent userAgent =  (UserAgent) getServletRequest().getAttribute(GlobalParam.USER_AGENT);
////		return webLogInfoMapper.loadByUserId(userAgent.getUserId());
////	}
//
//}
//>>>>>>> branch 'feature_2.7.0' of https://git.oschina.net/curryzhang/server_xunbaozl.git
